#include "stdafx.h"
#include "SegmentActions.h"

// SegmentTrimAction

bool SegmentTrimAction::Execute()
{
	if (!m_pBefore)
		m_pBefore = m_Segment.GetData();
	m_Segment.DoTrimDataSpan();
	return true;
}

void SegmentTrimAction::Unexecute()
{
	ASSERT(m_pBefore);
	m_Segment.SetData(m_pBefore);
}

// SegmentDataAction

void SegmentDataAction::After()
{
	if (!m_pBefore)
		m_pBefore = m_Segment.GetData();
	else
		ASSERT(m_pBefore == m_Segment.GetData());
	if (!m_pAfter)
		m_pAfter.reset(new SegmentData(* m_pBefore));
	m_Segment.SetData(m_pAfter);
}

void SegmentDataAction::Before()
{
	ASSERT(!m_Segment.IsProcessed()); // Pasha
	ASSERT(m_pBefore);
	ASSERT(m_pAfter);
	m_Segment.SetData(m_pBefore);
}

void SegmentDataAction::Unexecute()
{
	m_Segment.CancelProcessing(true);
	Before();
}

void SegmentDataAction::Reexecute()
{
	m_Segment.CancelProcessing(true);
	After();
}

// SegmentParamsAction

SegmentParamsAction::SegmentParamsAction(CXSegment & s, const CXParams & p) : 
	SegmentDataAction(s), m_Value(p) 
{ 
}

String SegmentParamsAction::GetCaption() const
{ 
	return "Change Parameters"; 
}

bool SegmentParamsAction::Execute() 
{
	m_Segment.CancelProcessing(true);
	After();
    if (!m_Segment.DoSetParams(m_Value)) {
		Before();
		return false;
	}
	return true;
}

// SegmentMaskAction

SegmentMaskAction::SegmentMaskAction(CXSegment & s, const BitMask & mask, const String & caption) : 
	SegmentDataAction(s), m_Value(mask), m_Caption(caption)
{ 
	if (m_Caption.empty())
		m_Caption = "Change Mask";
	m_OldValue = m_Segment.GetKeyMask();
}

String SegmentMaskAction::GetCaption() const
{ 
	return m_Caption;
}

bool SegmentMaskAction::Execute() 
{
	ASSERT(!m_Segment.IsProcessed());
	After();
	m_Segment.DoSetKeyMask(m_Value);
	return true;
}
